<?php

class LevelController extends Zend_Controller_Action {

    protected $_authNamespace;

    public function init() {
        /* Initialize action controller here */
        $this->_authNamespace = new Zend_Session_Namespace('Zend_Auth');
    }

    public function indexAction() {
        if ($this->_authNamespace->userRole == 'admin') {
            $level = new Application_Model_LevelMapper();
            $this->view->entries = $level->fetchAll();
        } else {
            return $this->_helper->redirector->gotoUrl('/auth/login');
        }
    }

    public function addLevelAction() {

        if ($this->_authNamespace->userRole == 'admin') {
            $request = $this->getRequest();
            $form = new Application_Form_Level();

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $level = new Application_Model_Level($form->getValues());
                    $mapper = new Application_Model_LevelMapper();
                    $mapper->save($level);
                    return $this->_helper->redirector('index');
                }
            }

            $this->view->form = $form;
        } else {
            return $this->_helper->redirector('index');
        }
    }

    public function editLevelAction() {
        if ($this->_authNamespace->userRole == 'admin') {

            $level_id = $this->_getParam("level_id");
            $request = $this->getRequest();
            $form = new Application_Form_Level();
            $this->view->level_id = $level_id;

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $data = array("id" => (int) $level_id);

                    $level = new Application_Model_Level($data);
                    $mapper = new Application_Model_LevelMapper();
                    $mapper->find($question_id, $level);
                    $level->setLevel($form->getValue('level'));
                    $level->setName($form->getValue('name'));
                    $mapper->save($level);
                    return $this->_helper->redirector('index');
                }
            } else {
                $data = array("id" => (int) $level_id);

                $level = new Application_Model_Level($data);

                $mapper = new Application_Model_LevelMapper();
                $mapper->find($level_id, $level);
                $form_data = array("id" => $level->getId(), "level" => $level->getLevel(), "name" => $level->getName());
                $form->populate($form_data);

                $testMapper = new Application_Model_TestMapper();

                $this->view->alltests = $testMapper->fetchAll();
                $this->view->tests = $testMapper->fetchByLevel((int) $level_id);
            }


            $this->view->form = $form;
        } else {
            return $this->_helper->redirector('index');
        }
    }

    public function deleteAction() {
        if ($this->_authNamespace->userRole == 'admin') {
            $level_id = $this->_getParam("level_id");
            $this->view->idValue = $level_id;
            if ($level_id != null) {
                $data = array("id" => (int) $level_id);

                $level = new Application_Model_Level($data);
                $mapper = new Application_Model_LevelMapper();
                $mapper->delete($level);

                return $this->_helper->redirector('index');
            }
        } else {
            return $this->_helper->redirector('index');
        }
    }

}

